package com.ruoyi.web.controller.yn;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.yn.domain.*;
import com.ruoyi.yn.service.*;
import com.ruoyi.yn.vo.YnUserOrderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author Ming
 * @since 2023-04-26 15:18:58
 */
@Api(tags = "小程序回收员相关")
@RestController
@RequestMapping("wx/recycler")
public class WxRecyclerController {

    @Resource
    private IYnRecyclerService recyclerService;

    @Resource
    private IYnUserOrderService ynUserOrderService;

    @Resource
    private IYnRecyclingTypeService recyclingTypeService;

    @Resource
    private IYnRecyclingWeightService recyclingWeightService;

    @Resource
    private IYnUserAddressService ynUserAddressService;


    @ApiOperation("回收员登录")
    @GetMapping("login")
    public R<YnRecycler> login(
            @ApiParam(value = "账号",required = true) @RequestParam("userName") String userName,
            @ApiParam(value = "密码",required = true) @RequestParam("password") String password
    ) {
        YnRecycler recycler = recyclerService.lambdaQuery().eq(YnRecycler::getUserName, userName).one();
        if (ObjectUtil.isEmpty(recycler)) {
            return R.fail("账号不存在");
        }
        if (!recycler.getPassword().equals(password)) {
            return R.fail("密码错误");
        }
        return R.ok(recycler);
    }

    @ApiOperation("更新回收员状态--更新经纬度及接单状态等")
    @PostMapping("changeInfo")
    public R<String> changeInfo(YnRecycler recycler) {
        if (ObjectUtil.isEmpty(recycler.getId())) {
            return R.fail("回收员ID未传入");
        }
        boolean success = recyclerService.updateById(recycler);
        return success ? R.ok() : R.fail("更新失败");
    }

    /**
     * 用户订单列表
     */
    @ApiOperation("回收员的订单列表-分页")
    @GetMapping("orderList/{wxUserId}")
    public R<Page<YnUserOrderVo>> orderList(
            @ApiParam("回收员ID") @PathVariable("recyclerId") String recyclerId,
            @ApiParam("第几页") @RequestParam("current") Long current,
            @ApiParam("每页条数") @RequestParam("size") Long size,
            @ApiParam("订单状态:0待处理1已处理2已取消3已评价") @RequestParam(value = "state",required = false) String state
    ) {
        Page<YnUserOrder> page = ynUserOrderService.lambdaQuery().eq(ObjectUtil.isNotEmpty(state), YnUserOrder::getState, state)
                .eq(YnUserOrder::getRecyclerId, recyclerId).page(new Page<>(current, size));
        Page<YnUserOrderVo> resultPage = new Page<>();
        resultPage.setTotal(page.getTotal());
        resultPage.setCurrent(page.getCurrent());
        resultPage.setSize(page.getSize());
        List<YnUserOrder> records = page.getRecords();
        Map<String, String> typeMap = recyclingTypeService.list().stream().collect(Collectors.toMap(YnRecyclingType::getId, YnRecyclingType::getName));
        Map<String, String> weightMap = recyclingWeightService.list().stream().collect(Collectors.toMap(YnRecyclingWeight::getId, YnRecyclingWeight::getWeight));
//        Map<String, YnUserAddress> ynUserAddresses = ynUserAddressService.lambdaQuery().eq(YnUserAddress::getWxUserId, wxUserId).list()
//                .stream().collect(Collectors.toMap(YnUserAddress::getId, Function.identity()));
        List<YnUserOrderVo> ynUserOrderVos = records.stream().map(o -> {
            YnUserOrderVo userOrderVo = BeanUtil.toBean(o, YnUserOrderVo.class);
            String typeNames = Arrays.stream(userOrderVo.getRecyclingTypeIds().split(",")).map(typeMap::get).collect(Collectors.joining(","));
            userOrderVo.setRecyclingTypeNames(typeNames);
            userOrderVo.setRecyclingWeightName(weightMap.get(userOrderVo.getRecyclingWeightId()));
            YnUserAddress userAddress = ynUserAddressService.getById(userOrderVo.getUserAddressId()); // 后期优化为关联查询
            userOrderVo.setYnUserAddress(userAddress);
            return userOrderVo;
        }).collect(Collectors.toList());
        resultPage.setRecords(ynUserOrderVos);
        return R.ok(resultPage);
    }


}
